package com.xuxin.baby.page;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xuxin.baby.R;
import com.xuxin.baby.activity.ClassGrowthActivity;
import com.xuxin.baby.activity.ReaderVideoSlidingActivity;
import com.xuxin.baby.adapter.HealthAdapter;
import com.xuxin.baby.adapter.HealthExtendAdapter;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.base.BaseFragment;
import com.xuxin.baby.bean.CategoryBean;
import com.xuxin.baby.contract.CategoryContract;
import com.xuxin.baby.presenter.CategoryPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;

/**
 * 儿童保健
 */
public class HealthFragment extends BaseFragment implements CategoryContract.View {
    private final CategoryContract.Presenter presenter = new CategoryPresenter(this);
    @BindView(R.id.health_refresh)
    SmartRefreshLayout health_refresh;
    @BindView(R.id.health_recycle)
    RecyclerView health_recycle;
    @BindView(R.id.health_recycle_extend)
    RecyclerView health_recycle_extend;

    private HealthExtendAdapter myAdapter;//儿童保健
    private String tenantId;//租户id
    private String token;//授权token

    public static HealthFragment newInstance() {
        return new HealthFragment();
    }

    @Override
    public View setContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_health, container, false);
    }

    @Override
    public void initView() {
        health_refresh.setEnableLoadMore(false);
        health_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onRefresh");
                handler.sendEmptyMessage(0);
            }
        });

        RecycleUtils.init(health_recycle, 3);
        HealthAdapter healthAdapter = new HealthAdapter();
        health_recycle.setAdapter(healthAdapter);
        healthAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                switch (position) {
                    case 0:
                        ActivityUtils.startActivity(ClassGrowthActivity.class);
                        break;
                    case 1:
                        ToastUtils.showShort("功能完善中...");
                        break;
                }
            }
        });

        RecycleUtils.init(health_recycle_extend, 3);
        myAdapter = new HealthExtendAdapter();
        health_recycle_extend.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (!myAdapter.getData().get(position).isHide()) {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", myAdapter.getData().get(position).getCategoryName());
                    bundle.putString("categoryId", myAdapter.getData().get(position).getId());
                    ActivityUtils.startActivity(bundle, ReaderVideoSlidingActivity.class);
                }
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(getContext());
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {
        health_refresh.finishRefresh();
        health_refresh.finishLoadMore();
    }

    @Override
    public void onClick(View view) {

    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getCategory(tenantId, token, "course", BaseConstant.categoryId_health);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(CategoryBean categoryBean) {
        health_recycle_extend.setVisibility(View.VISIBLE);
        if (null != categoryBean.getResult() && categoryBean.getResult().size() > 0) {
            myAdapter.setList(categoryBean.getResult());
        }
    }
}